package cn.neusoft.controller;

import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBaseService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

public class BaseController<T,W>{

    @Autowired
    IBaseService<T> baseService;

    @ApiOperation(value = "保存数据，有id就修改，没id就添加" )
    @PostMapping      //  请求方式 + 请求路径   post  /模块路径[根据继承类型的模块路径来确定]
    public ResultBean save(@RequestBody T t){
        System.out.println("----BaseController-----save--------");
        System.out.println(t);
        return baseService.save(t);
    }

    @ApiOperation(value = "通过id删除" )
    //  delete   /xxx/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/xxx/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------BaseController--------del-------------");
        System.out.println(id);
        return baseService.delete(id);
    }

    @ApiOperation(value = "通过id查询" )
    //  get   /xxx/1004
    @GetMapping("/{id}")   // http://localhost:8080/xxx/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------BaseController--------getById-------------");
        System.out.println(id);
        return baseService.getById(id);
    }

    @ApiOperation(value = "分页条件搜索" )
    @PostMapping("/all")    //  post  /xxx/all
    public <W extends BaseQuery> ResultBean getAll(@RequestBody W baseQuery){
        System.out.println("-------BaseController-----getAll--------");
        return baseService.getAll(baseQuery);
    }

    @ApiOperation(value = "批量删除" )
    @PostMapping("/batch")   // post + /xxx/batch
    public ResultBean batchDel(@RequestBody List<Integer> ids){
        System.out.println("----------BaseController---------batchDel---------------");
        return baseService.batchDel(ids);
    }

}
